<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
</head>
<style>
    .layui-footer {
        padding: 10px 0;
        text-align: center;
    }
    .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="component-form-element">
                <div class="layui-tab-content" style="height: auto">
                    <div class="layui-tab-item layui-show" id="onecontent">
                        <input type="hidden" name="tid" value="${user.tid}">
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">旧密码</label>
                                    <div class="layui-input-block">
                                        <input type="password" name="oldpassword" lay-verify="oldpassword"
                                               autocomplete="off" placeholder="请输入旧密码"
                                               class="layui-input" id="oldpassword">
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">新密码</label>
                                    <div class="layui-input-block">
                                        <input type="password" name="newpassword" id="newpassword"
                                               autocomplete="off" placeholder="请输入新密码"
                                               class="layui-input" lay-verify="newpassword">
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-col-lg6">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">确认新密码</label>
                                    <div class="layui-input-block">
                                        <input type="password" name="renewpassword"
                                               autocomplete="off" placeholder="请确认新密码"
                                               class="layui-input" lay-verify="renewpassword">
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-footer"<%-- style="left: 0;padding: 0px;"--%>>
                                <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">提交更改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script>
    layui.use(['form', 'laydate'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            oldpassword: function (value, item) {
                if (value === "") {
                    return '请输入旧密码';
                }
            },
            newpassword: function (value, item) {
                if (value === "") {
                    return '请输入新密码';
                }
            },
            renewpassword: function (value, item) {
                var newpassword = $("#newpassword").val();
                if (value === "") {
                    return '请再次输入新密码';
                }else if(newpassword !== value){
                    return '两次输入新密码不正确';
                }
            },
        });

        //第一种方法添加用户
        form.on('submit(*)', function (data) {
            var field = data.field;//当前容器的全部表单字段，名值对形式：{name: value}
            var oldpassword = field.oldpassword;
            var flag = null;
            $.ajax({
                type: "get",
                url: "<%=request.getContextPath()%>/rest/system/verifyUserPassword",
                dataType: "json",
                async: false,
                data: "password=" + oldpassword + "&tid=" + field.tid,
                success: function (msg) {
                    flag = msg.res;
                }
            });
            if (flag === "true") {
                var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
                $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/system/updateUserPassword",
                    data: field,
                    dataType: "json",
                    async: false,
                    success: function (obj) {
                        setTimeout(function () {
                            //layer.close(index);
                            if (obj === true) {
                                layer.msg('更改成功');
                                layer.open({
                                    type: 1
                                    ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                                    ,id: 'layerDemo'+'auto' //防止重复弹出
                                    ,content: '<div style="padding: 20px 100px;">更改成功</div>'
                                    ,btn: '确认'
                                    ,btnAlign: 'c' //按钮居中
                                    ,shade: 1 //不显示遮罩
                                    ,yes: function(){
                                        //向服务端发送删除指令
                                        $.ajax({
                                            type: "post",
                                            url: "<%=request.getContextPath()%>/rest/logOut",
                                            dataType: "json",
                                            async: false,
                                            success: function (msg) {
                                                location.href = "<%=request.getContextPath()%>/login.jsp";
                                            }
                                        });
                                    }
                                });
                            } else {
                                layer.msg(obj.errorMsg);
                            }
                        }, 2000);
                    }
                });
            } else {
                layer.msg(flag, {icon: 5, anim: 6});
            }
            return false;
        });
    });
</script>
</body>
</html>